package com.lanchetech.admin.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanchetech.admin.service.AdminService;
import com.lanchetech.admin.service.ArticleService;
import com.lanchetech.bean.request.PageReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.util.PageHelpUtil;
import com.lanchetech.common.enums.ResultCode;
import com.lanchetech.common.util.EmojiFilter;
import com.lanchetech.dao.ArticleMapper;
import com.lanchetech.dao.CompanyIntroductionMapper;
import com.lanchetech.dao.SpuMapper;
import com.lanchetech.entity.Article;
import com.lanchetech.entity.CompanyIntroduction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    AdminService adminService;

    @Autowired
    SpuMapper spuMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    CompanyIntroductionMapper companyIntroductionMapper;

    @Override
    public ResultData<Article> getArticleDetail(Long id) {
        return new ResultData<>(articleMapper.selectByPrimaryKey(id));
    }

    @Override
    public BaseResp editArticle(Article article) {
        if (article.getId() == null) {
            // 新增
            article.setCreatedAt(new Date());
            article.setUpdatedAt(new Date());
            article.setCanDelete(1);
            articleMapper.insert(article);
        } else {
            // 编辑
            article.setCanDelete(null);
            article.setUpdatedAt(new Date());
            articleMapper.updateByPrimaryKeySelective(article);
        }

        redisTemplate.delete("HomeInfoResp");
        return new BaseResp();
    }

    @Override
    public ResultData<BasePageResp<Article>> getArticlePage(PageReq req) {
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<Article> list = articleMapper.findAllByArticle(req);
        return PageHelpUtil.buildPage(list, page);
    }

    @Override
    public BaseResp deleteArticle(Long id) {
        Article article = articleMapper.selectByPrimaryKey(id);
        if (article.getCanDelete() == 1) {
            articleMapper.deleteByPrimaryKey(id);
        } else {
            new BaseResp(ResultCode.FAIL);
        }
        return new BaseResp();
    }

    @Override
    public BaseResp editArticleEffect(Long id, String effect) {
        if(effect == null){
            effect = "";
        }
        articleMapper.updateEffectById(effect,id);
        return new BaseResp();
    }

    @Override
    public BaseResp editCompanyIntroduction(CompanyIntroduction req) {
        req.setId(1L);
        req.setUpdatedAt(new Date());
        companyIntroductionMapper.updateByPrimaryKeySelective(req);
        return new BaseResp();
    }

    @Override
    public ResultData<CompanyIntroduction> getCompanyIntroduction() {
        return new ResultData<>(companyIntroductionMapper.selectByPrimaryKey(1L));
    }
}